package com.zhijiaoyun.common.email;

import com.zhijiaoyun.common.JSONUtils;
import com.zhijiaoyun.model.User;

import java.util.Base64;
import java.util.Calendar;
import java.util.Date;

/**
 * @author shisfish
 * @date 2017/6/6
 */
public class EmailModal {

    public static String URL = "http://www.zhijiaolearn.com/";
    public static String PROJECT_NAME = "zhijiaoyun-java";

    public static String createRegisterModal(String email, User user) {
        String code = createCode(email, user);
        StringBuffer sb = new StringBuffer("点击下面链接激活账号，24小时生效，否则重新注册账号，链接只能使用一次，请尽快激活！</br>");
        sb.append("<a href=\"" + URL + PROJECT_NAME + "/api/user/register?action=email&code=");
        sb.append(code);
        sb.append("\">" + URL + PROJECT_NAME + "/api/user/register?action=email&code=");
        sb.append(code);
        sb.append("</a>");
        return sb.toString();
    }

    public static String createForgetPasswordModal(String email) {
        String code = createCode(email);
        StringBuffer sb = new StringBuffer("点击下面链接去修改密码，1小时生效，链接只能使用一次，请尽快激活！</br>");
        sb.append("<a href=\"" + URL + PROJECT_NAME + "/api/user/forgetPasswordConfirm?action=forgetPassword&code=");
        sb.append(code);
        sb.append("\">" + URL + PROJECT_NAME + "/api/user/forgetPasswordConfirm?action=forgetPassword&code=");
        sb.append(code);
        sb.append("</a>");
        return sb.toString();
    }

    public static String createCode(String email) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.HOUR_OF_DAY, 1);
        String json = "{\"email\": \"" + email + "\", \"expDate\": \"" + cal.getTimeInMillis() + "\"}";
        return json;
    }

    public static String createCode(String email, User user) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_YEAR, 1);
        String jsonString = "{\"email\": \"" + email + "\", \"expDate\": " + cal.getTimeInMillis() + ", \"user\": "
                + JSONUtils.object2JsonString(user) + "}";
        return Base64.getEncoder().encodeToString(jsonString.getBytes());
    }

}
